<template>
  <div class="search-skill">
    <div class="top-box">
      <div class="nav-bar">
        <div class="nav-left">
          <i
            class="iconfont icon-zuojiantou"
            @click="$router.push('/skill')"
          ></i>
        </div>
        <div class="nav-title">面试技巧搜索</div>
      </div>
      <div class="search-box">
        <form action="/">
          <van-search
            v-model="value"
            show-action
            placeholder="请输入搜索关键词"
            shape="round"
            @search="onSearch"
            @cancel="onCancel"
            @clear="onCancel"
          />
        </form>
      </div>
    </div>

    <div v-if="searchList.length == 0" class="option-box">
        <div class="section">
            <div class="title">大家都在搜索</div>
            <ul class="tags">
                <li class="tag" @click="hotClick(item)" v-for="(item,index) in topsearch" :key="index">{{item}}</li>
            </ul>
        </div>
        <div class="section">
            <div class="title">历史记录<span @click="clear">清空</span></div>
            <ul class="tags">
                <li class="tag" @click="hotClick(item)" v-for="(item,index) in history" :key="index">{{item}}</li>
            </ul>
        </div>
    </div>

    <div v-else class="skill-list">
      <div class="skill-item" @click="$router.push(`/skilldetail/${item.id}`)" v-for="item in searchList" :key="item.id">
        <div class="content">
          <div class="title">{{ item.title }}</div>
          <div class="info">
            <div class="time">{{ formatTime(item.created_at) }}</div>
            <div class="see">
              <van-icon name="eye-o" size="17px" /><span>{{ item.read }}</span>
            </div>
            <div class="zan">
              <van-icon
                name="good-job-o"
                size="17px"
                :class="{ active: item.star != 0 }"
              /><span>{{ item.star }}</span>
            </div>
          </div>
        </div>
        <div v-if="item.cover != null" class="cover">
          <img  :src="baseURL + item.cover" alt="" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { topSearch, skillList } from '@/api/skill'
export default {
  data () {
    return {
      value: '',
      topsearch: [],
      history: [],
      skillList: [],
      searchList: []
    }
  },
  created () {
    this.loadTopSearch()
    this.loadSkillList()
  },
  methods: {
    onSearch (val) {
      val = this.value
      this.skillList.forEach(v => {
        if (v.title.includes(val)) {
          this.searchList.push(v)
        }
      })
    },
    async loadSkillList () {
      const res = await skillList()
      this.skillList = res.data.list
    },
    formatTime (date) {
      return this.dayjs(date).format('YYYY.MM.DD')
    },
    onCancel () {
      this.searchList = []
    },
    async loadTopSearch () {
      if (localStorage.getItem('history')) {
        this.history = Object.values(JSON.parse(localStorage.getItem('history')))
      }
      const res = await topSearch()
      this.topsearch = res.data
    },
    hotClick (value) {
      this.value = value
      if (!this.history.includes(value)) {
        this.history.push(value)
      }
      localStorage.setItem('history', JSON.stringify(this.history))
      this.onSearch()
    },
    clear () {
      this.history = []
      localStorage.removeItem('history')
    }
  }
}
</script>

<style lang="less" scoped>
.search-skill {
  .top-box {
    padding-bottom: 10px;
    padding-top: 5px;
    box-shadow: 0 0.05333rem 0.10667rem 0 rgb(24 26 57 / 4%);
    .nav-bar {
      position: relative;
      z-index: 1;
      display: flex;
      align-items: center;
      height: 40px;
      line-height: 1.5;
      text-align: center;
      background-color: #fff;
      .nav-left {
        position: absolute;
        top: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        padding: 0 10px;
        .iconfont {
          font-size: 25px;
        }
      }
      .nav-title {
        margin: 0 auto;
        font-size: 18px;
      }
    }
  }
  .option-box {
    padding: 12px 15px;
    .section {
        margin-bottom: 25px;
        .title {
            font-size: 17px;
            text-align: left;
            display: flex;
            justify-content: space-between;
            align-items: center;
            span {
                font-size: 12px;
            }
        }
        .tags {
            margin-top: 18px;
            display: flex;
            .tag {
                margin-right: 10px;
                margin-bottom: 6px;
                padding: 6px;
                border-radius: 4px;
                font-size: 12px;
                color: #545671;
                background-color: #eceaea;
            }
        }
        ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }
    }
  }
  .skill-list {
    padding: 0px 15px;
    .skill-item {
      display: flex;
      padding-bottom: 30px;
      font-size: 16px;
      .content {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .title {
          text-align: left;
        }
        .info {
          color: #b4b4bd;
          font-size: 12px;
          display: flex;
          align-items: center;
          margin-top: 10px;
          span {
            margin-left: 8px;
          }
          .time {
            flex: 1;
            text-align: left;
          }
          .see {
            display: flex;
            align-items: center;
            margin-right: 25px;
          }
          .zan {
            display: flex;
            align-items: center;
            .active {
              color: red;
            }
          }
        }
      }
      .cover {
        width: 113px;
        height: 75px;
        margin-left: 20px;
        img {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
}
</style>
